package com.tobdev.qywxinner.service;

import com.tobdev.qywxinner.mapper.QywxInnerUserMapper;
import com.tobdev.qywxinner.model.dto.WxUserDetailResponseDto;
import com.tobdev.qywxinner.model.entity.QywxInnerUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class QywxInnerUserService {

    @Autowired
    private QywxInnerUserMapper qywxInnerUserMapper;


    public QywxInnerUser getUserByCorpIdAndUserId(String corpId, String userId) {
        QywxInnerUser user = qywxInnerUserMapper.getUserByCorpIdAndUserId(corpId, userId);
        if (user == null) return null;
        return user;
    }

    public Integer saveUser(QywxInnerUser user) {
        return qywxInnerUserMapper.saveUser(user);
    }


    public Boolean deleteUserByCorpId(String corpId) {
        Integer rs = qywxInnerUserMapper.deleteUserByCorpId(corpId);
        if (rs >= 0) {
            return true;
        }
        return false;
    }

    public QywxInnerUser getOrInsertByCorpIdAndUserId(String corpId, WxUserDetailResponseDto userDetailResponseDto) {
        QywxInnerUser user = this.getUserByCorpIdAndUserId(corpId, userDetailResponseDto.getUserid());
        if (null == user) {
            user = new QywxInnerUser();
            user.setCorpId(corpId);
            user.setUserId(userDetailResponseDto.getUserid());
            user.setName(userDetailResponseDto.getName());
            user.setAvatar(userDetailResponseDto.getAvatar());
            user.setMobile(userDetailResponseDto.getMobile());
            user.setQrCode(userDetailResponseDto.getQr_code());
            // todo 什么意思
            user.setUserType(0);
            qywxInnerUserMapper.insertUser(user);

        }
        return user;
    }
}
